Skip to content

FRON-12153 Add url-based cache eviction and cache TTL (maxAge option)#76

Open
polinazolotukhina wants to merge 4 commits into
d1opensource:masterfrom
polinazolotukhina:FRON-12153-cache-ttl-maxage
Open

FRON-12153 Add url-based cache eviction and cache TTL (maxAge option)#76
polinazolotukhina wants to merge 4 commits into
d1opensource:masterfrom
polinazolotukhina:FRON-12153-cache-ttl-maxage

Conversation

@polinazolotukhina

@polinazolotukhina polinazolotukhina commented Jul 3, 2026

Copy link
Copy Markdown

Summary

  • Adds deleteCachedResponsesByUrl(urlPrefix) method to Api — removes all cached responses whose request url matches the given prefix, so the next request for any of them fetches fresh data
    • Matching by url (rather than by request params) covers cache keys that cannot be reconstructed at the call site — most commonly keys that embed a serialized request body via extraKey (e.g. data-services snapshot endpoints), which only the original caller can produce
    • Matching stops at path/query boundaries: /users matches /users, /users/1, and /users?page=2, but not /users-archive
  • Adds maxAge option to ApiRequestOptions and UseApiQueryOptions — cached entries older than the specified milliseconds are treated as a cache miss, triggering a fresh fetch
  • Expired entries are evicted from the cache on read to prevent memory leaks
  • Adds parseApiRequestIdUrl helper in lib, co-located with apiRequestId so both directions of the cache-key format live in one module
  • Fixes readCachedResponse docs: returns null on cache miss, not undefined

Usage

// Invalidate cached responses after a mutation — evicts the plain list,
// filtered/paginated variants, and POST-based query endpoints whose
// cache keys embed the serialized request body
await api.request(PortfolioGroupEndpoints.update(id, body))
api.deleteCachedResponsesByUrl('/v3/groups/portfolio-groups')
api.deleteCachedResponsesByUrl('/v3/data-services/portfolio-groups')

// Cache with TTL — define as a shared constant per endpoint
export const BROKERS_MAX_AGE = 60_000

api.request(params, { fetchPolicy: 'cache-first', maxAge: BROKERS_MAX_AGE })

useApiQuery(params, { fetchPolicy: 'cache-first', maxAge: BROKERS_MAX_AGE })

Test plan

  • deleteCachedResponsesByUrl removes all entries matching the url prefix — next requests go to network
  • Matches sub-paths (/users/1) and query variants (/users?page=2) of the prefix
  • Does not evict urls that extend the prefix without a path boundary (/users-archive)
  • Does not affect entries with non-matching urls
  • Is a no-op when no cached entries match
  • parseApiRequestIdUrl round-trips the url from apiRequestId
  • Cache hit within maxAge — no network request fired
  • Cache miss after maxAge expires — network request fires and cache is refreshed
  • No maxAge specified — existing cache behaviour unchanged
  • Expired entry is removed from cache on read (no memory leak)
  • Verified end-to-end in the UI app: admin Portfolio Groups list serves from cache on revisit, and refetches after a portfolio group is created/updated/deleted

polina and others added 3 commits June 26, 2026 12:50
Adds a `maxAge` option to `ApiRequestOptions` that treats cached entries
older than the given milliseconds as a cache miss, triggering a fresh fetch.
Expired entries are evicted from the cache on read to prevent memory leaks.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Threads the maxAge option through to api.request so the hook respects
cache TTL the same way direct api.request calls do.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@polinazolotukhina polinazolotukhina changed the title FRON-12153 Add cache TTL (maxAge option) FRON-12153 Add deleteCachedResponse and cache TTL (maxAge option) Jul 3, 2026
deleteCachedResponse(params) required reconstructing the exact request
params to compute the cache key, which is impossible for cache keys that
embed a serialized request body via extraKey. deleteCachedResponsesByUrl
matches entries by url prefix instead, evicting whole request families
(sub-paths and query variants) while stopping at path boundaries.
@polinazolotukhina polinazolotukhina changed the title FRON-12153 Add deleteCachedResponse and cache TTL (maxAge option) FRON-12153 Add url-based cache eviction and cache TTL (maxAge option) Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant